package com.demo2do.fms.admin.web.controller.data.cost;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.fms.admin.service.data.DefaultOilTypeService;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.entity.data.cost.DefaultOilType;

/**
 * 默认加油类型
 * 
 * @author Wy
 *
 */
@Controller
@MenuSetting("menu-data")
@RequestMapping("/default-oil-types")
public class DefaultOilTypeController {
    
    private static final Logger logger = LoggerFactory.getLogger(DefaultOilTypeController.class);
    
    @Autowired
    private DefaultOilTypeService defaultOilTypeService;
    
    /**
     * list default oil type
     * 
     * @param orgIds
     * @return
     */
    @MenuSetting("submenu-default-oil-type")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView list(@Secure(property = "orgIds") List<Long> orgIds){
        
        ModelAndView modelAndView = new ModelAndView("data/cost/default-oil-type/default-oil-type-list");
        
        int totals = defaultOilTypeService.count(orgIds);
        if (totals > 0) {
        	
        	List<DefaultOilType> defaultOilTypes = defaultOilTypeService.list(orgIds);
        	modelAndView.addObject("defaultOilTypes", defaultOilTypes);
        	
        }
        
        modelAndView.addObject("orgIds", orgIds);
        
        return modelAndView;
        
    }
    
    /**
     * search
     * 
     * @param orgIds
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ModelAndView search(@Secure(property = "orgIds") List<Long> orgIds){
        
        ModelAndView modelAndView = new ModelAndView("data/cost/default-oil-type/default-oil-type-search");
        
        modelAndView.addObject("orgIds", orgIds);
        
        return modelAndView;
        
    }
    
    /**
     * on search
     * 
     * @param orgIds
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params= "filter")
    @MenuSetting("submenu-default-oil-type")
    public ModelAndView onSearch(@RequestParam(value = "orgIds") List<Long> orgIds){
        
        ModelAndView modelAndView = new ModelAndView("data/cost/default-oil-type/default-oil-type-list");
        
        List<DefaultOilType> defaultOilTypes = defaultOilTypeService.list(orgIds);
        
        modelAndView.addObject("defaultOilTypes", defaultOilTypes);
        
        return modelAndView;
        
    }
    
    /**
     * display edit panel
     * 
     * @param orgId
     * @return
     */
    @RequestMapping(value = "/{orgId}/edit", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("orgId") Long orgId){
        
        ModelAndView modelAndView = new ModelAndView("data/cost/default-oil-type/default-oil-type-edit");
        
        DefaultOilType defaultOilType = defaultOilTypeService.load(orgId);
        
        modelAndView.addObject("defaultOilType", defaultOilType);
        
        return modelAndView;
        
    }
    
    /**
     * on edit
     * 
     * @param orgId
     * @param oilTypeId
     * @return
     */
    @MenuSetting("submenu-default-oil-type")
    @RequestMapping(value = "/{orgId}", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEdit(@PathVariable("orgId") Long orgId,
                                           @RequestParam("oilTypeId") Long oilTypeId){
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            defaultOilTypeService.edit(orgId, oilTypeId);
            
            httpResult.success().message("设置成功");
            
        } catch (Exception e) {
            httpResult.fail("500").message("设置失败");
            logger.error("#onEdit() - edit oil type fail, exception is [{}]", e);
        }
        
        return httpResult;
        
    }
    
}